package com.frame.pageform.tag;

import java.util.Iterator;
import com.frame.pageform.Iterate;

public class IterateTag extends BaseTag{

	private String id;
	private String bind;
	private Iterator iterator;
	private String indexId;
	private String type;
	private int index;

	public int start() throws Exception
	{
		Object value = executeExpression(bind);
		if (value == null)
			return 0;
		index=0;
		iterator=Iterate.getIterator(value);

		if (next())
			return EVAL_BODY_INCLUDE;
		else
			return 0;
	}
	public int afterBody() throws Exception{
		if (next())
			return EVAL_BODY_AGAIN;
		else
			return 0;
	}
	public int end() throws Exception
	{
		iterator = null;
		return 0;
	}
	private boolean next()
	{
		if (iterator.hasNext())
		{Object obj=iterator.next();
			setVariable(id, obj);
			if (indexId != null)
				setVariable(indexId, new Integer(index++));
			return true;
		}
		else
		{
			return false;
		}
	}

	public void release()
	{
		super.release();
		iterator = null;
	}

	public String getBind() {
		return bind;
	}
	public void setBind(String bind) {
		this.bind = bind;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getIndexId() {
		return indexId;
	}
	public void setIndexId(String indexId) {
		this.indexId = indexId;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}


}
